package com.sram.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**导出excel 文件
 * 导入4个jar包
 * poi-3.10-FINAL-20140208.jar、、、poi-ooxml-3.9-20121203.jar、、、poi-ooxml-schemas-3.9-20121203.jar
 * 和xmlbeans-2.3.0.jar-------(new XSSFWorkbook(inputStream)的时候用)
 * 必须有getIndexExportOrderExcelSj和getExcelFileName
 * Struts
 * <action name="exportByHbx" class="exportAction"  >
 *	     <result type="stream">
 *		<param name="contentType">application/vnd.ms-excel</param>
 *			<param name="inputName">indexExportOrderExcelSj</param>
 *			<param name="contentDisposition">attachment; filename="${excelFileName}"</param>
 *</result>
 *</action>
 * 
 * 
 * 
 * 
 */
import com.sram.util.ExcelUtil;
@Transactional(
		isolation=Isolation.DEFAULT,
		propagation=Propagation.REQUIRED,
		readOnly=false,
		noRollbackFor=ArithmeticException.class,
		rollbackFor=NullPointerException.class,
		timeout=-1)
public class ImportAction extends BaseAction{
	private String fileName;
	private File file;
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String saveImport(){
		String fileMsg = "";
		String importFileName = this.getRequest().getParameter("fileName");
		if(importFileName!=null && importFileName.length()>0){
			try {
				int index = importFileName.indexOf(".");
				String extentName = importFileName.substring(index + 1).toLowerCase();
				if("xlsx".equals(extentName)){
					InputStream is = new FileInputStream(this.file);
					fileMsg = Import(is,true );
				}else if("xls".equals(extentName)){
					InputStream is = new FileInputStream(this.file);
					fileMsg = Import(is,false );
				}else {
					fileMsg = "请上传Excel文件!";
				}
			} catch (Exception e) {
				fileMsg = "导入出现异常，请检查您的文件!";
			}
		}else {
			fileMsg = "请上传Excel文件!";
		}
		this.getRequest().setAttribute("fileMsg", fileMsg);
		
		return "ImportSUCCESS";
	
	}
	/**
	 * 导入excel
	 */
	public String Import(InputStream inputStream, boolean isTrue)throws Exception {
		Workbook wb = null;
		Sheet sheet = null;
		int successCount = 0;
		int failCount = 0;
		int totalCount = 0;
		if(isTrue){
			wb = new XSSFWorkbook(inputStream);
			sheet = wb.getSheetAt(0);
		}else {
			wb = new HSSFWorkbook(inputStream);
			sheet = (HSSFSheet)wb.getSheetAt(0);
		}
		Iterator<Row> iterator = sheet.rowIterator();
		iterator.next();
		while(iterator.hasNext()){
			Row row = iterator.next();
			if(ExcelUtil.getValue(row.getCell(0)) != null && !"".equals(ExcelUtil.getValue(row.getCell(0)).toString())){
				try {
					String username = ExcelUtil.getValue(row.getCell(0)).toString();
					String password = ExcelUtil.getValue(row.getCell(1)).toString();
					System.out.println("导入成功");
					System.out.println(username);
					System.out.println(password);

					//导入的商家名 ，如果存在并且是商家的就保存，不是的不保存
					successCount += 1;
					totalCount += 1;
				} catch (Exception e) {
					failCount += 1;
					totalCount += 1;
				}
			}
		}
		String flg = "总共"+totalCount+"条数据；成功"+successCount+"条；失败"+failCount+"条!";
		return flg;
	}

}








